package com.g.places.api.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpClient;

import com.g.places.api.model.GPlace;
import com.g.places.api.model.GReferencePlaces;
import com.g.places.api.util.Parameter;
import com.g.places.api.util.URLValidatorUtil;

/**
 * Default Google Places Client that implement Places Client 
 * currently you can make two types of fetching using HTTP Client / JSON  
 * <ul>
 * <li>
 * Place Search Requests by text searching </li>
 * <li>
 * Place Details by reference</li>
 * </ul>
 * 
 * @author ahmad
 */

public class DefaultPlacesClient extends AbstractPlaceClient {

	
	// default constructor by given http Client to make connection / requests 
	public DefaultPlacesClient(HttpClient httpClient) {
		
		// call Abstract Place Client constructor 
		super(httpClient);
		
	}
	
	
	/**
	 * Place Search Requests by text searching with given search key and your
	 * API key that you get from google places API GPlace represent Place Search
	 * Requests Objects
	 * 
	 * @param searchStr
	 *            search string key for searching places on google places
	 * @param apiKey
	 *            google places API Key for using Google Places API services
	 * @param nextPageRef
	 *            for next set of result (next page reference) , optional
	 *            parameter
	 * @return GPlace object represent google places results
	 * 
	 * NOTE please There is a short delay when use nextPageRef 
	 * RESULT SET - ENGLISH LANGUAGE  
	 */
	public GPlace fetchGPlacesByTextSearching(String searchStr,
			SimpleApiAccess apiKey, String nextPageRef) {

		// default parameters 
		List<Parameter> parameters = new ArrayList<>(Arrays.asList(
				Parameter.addParam("sensor", "false"),
				Parameter.addParam("query",
						URLValidatorUtil.validSearchStr(searchStr)),
				Parameter.addParam("language",LanguageCode.ENGLISH.getCode()),
				Parameter.addParam("key", URLValidatorUtil.getAPIKey(apiKey))));

		// next page reference
		if (StringUtils.isNotBlank(nextPageRef)) {
			parameters.add(Parameter.addParam("pagetoken", nextPageRef));
		}

		return fetchGPlaces(
		// base url for searching place by text
				SEARCH_TEXT_PLACES_BASE_URL,

				// output JSON
				OutputType.JSON,

				// class type
				GPlace.class,
				
				// convert parameters to array
				parameters.toArray(new Parameter[parameters.size()])

		);

	}

	/**
	 * Place Details by reference  with given reference key string and your API key 
	 * that you get from google places API  
	 * GReferencePlaces represent Place Details  
	 * @param reference 
	 * 			reference string key for searching for specific place  
	 * @param apiKey 
	 * 			google places API Key for using Google Places API services 
	 * @return GReferencePlaces object represent google place details
	 * NOTE : RESULT SET - ENGLISH LANGUAGE
	 */
	public GReferencePlaces fetchGDetailsPlacesByTextSearching(
			String reference, SimpleApiAccess apiKey) {
		
		
		return fetchGPlaces(
				// base url for searching place details 
				SEARCH_TEXT_PLACE_DETAILS_BASE_URL,
				// output JSON 
				OutputType.JSON,
				// class type 
				GReferencePlaces.class,
				
				// required parameters 
				// sensor = false ...
				Parameter.addParam("sensor", "false"),
				Parameter.addParam("reference", reference ),
				Parameter.addParam("key", apiKey.getgAPIKey()),
				Parameter.addParam("language",LanguageCode.ENGLISH.getCode())
				
				);
	}

	
	
	
}
